package ui.views.property;

import java.util.Calendar;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.tabbed.AbstractPropertySection;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ui.tempdao.EgnProductDao;
import ui.util.CalendarUtil;
import ui.util.IStructuredViewer;
import dao.EgnProduct;
import dao.EgnRstProductVarmap;
import de.ralfebert.rcputils.builder.table.ColumnBuilder;
import de.ralfebert.rcputils.builder.table.TableViewerBuilder;
import de.ralfebert.rcputils.builder.table.format.Formatter;
import de.ralfebert.rcputils.properties.BaseValue;

public class ProductVarmapSection extends AbstractPropertySection implements IStructuredViewer{
	private final static Logger logger = LoggerFactory.getLogger(ProductVarmapSection.class);

	private EgnRstProductVarmap prodVarmap;
	private EgnProduct prod;
	private TableViewer tableViewer;

	@Override
	public TableViewer getTableViewer(){
		return tableViewer;
	}
	@Override
	public TreeViewer getTreeViewer(){
		return null	;
	}
	@Override
	public void setInput(IWorkbenchPart part, ISelection selection) {
		super.setInput(part, selection);
		Object input = ((IStructuredSelection) selection).getFirstElement();
		if( input instanceof EgnProduct ){
			this.prod = (EgnProduct) input;
			tableViewer.setInput(EgnProductDao.getVarmap(prod.getProductId()));
		}
		Formatter.setZebraBackground(tableViewer);
	}

	@Override
	public void createControls(Composite parent, TabbedPropertySheetPage aTabbedPropertySheetPage) {
		super.createControls(parent, aTabbedPropertySheetPage);
		createTable(parent);
		
	}
//	@Override
//	public void refresh() {
		// labelText.removeModifyListener(listener);
		// ButtonElementProperties properties = (ButtonElementProperties)
		// buttonElement
		// .getAdapter(IPropertySource.class);
		// labelText.setText(properties.strText);
		// labelText.addModifyListener(listener);
//	}
	
	private void createTable(Composite parent){
		TableViewerBuilder t = new TableViewerBuilder(parent);

		ColumnBuilder baseDate = t.createColumn("Base Date");
		baseDate.bindToValue(new BaseValue<EgnRstProductVarmap>() {
			@Override
			public Object get(EgnRstProductVarmap varmap){
				Calendar cal = varmap.getId().getBaseDate();
				return CalendarUtil.toString(cal);
			}
		});
		baseDate.build();
	
		ColumnBuilder id = t.createColumn("Product Id");
		id.bindToProperty("id.productId");
		id.useAsDefaultSortColumn();
		id.build();
		
		ColumnBuilder type = t.createColumn("Risk Factor");
		type.bindToProperty("id.riskFactorId");
		type.build();
		
		ColumnBuilder keyRatio = t.createColumn("Key Ratio Duration");
		keyRatio.bindToProperty("keyRatioDuration");
		keyRatio.build();
		
		ColumnBuilder delta = t.createColumn("Delta Equivalent");
		delta.bindToProperty("deltaEquivalent");
		delta.build();
		
		ColumnBuilder varmap = t.createColumn("VaR Map");
		varmap.bindToProperty("varMap");
		varmap.build();
		
		t.setInput(null);
		tableViewer = t.getTableViewer();
		
//		GridData gridData =new GridData();
//		gridData.grabExcessVerticalSpace =true;
//		tableViewer.getTable().setLayoutData(gridData);
	}
}
